package com.power.test.model.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * LocalDateTime 序列化器
 */
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
    public LocalDateTimeSerializer() {

    }

    @Override
    public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        //毫秒
        //Long time =localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        //秒
        Long time = localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
        jsonGenerator.writeObject(time);
    }
}
